package androidx.test.espresso.action;

import android.os.Build;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import androidx.core.os.EnvironmentCompat;
import androidx.test.espresso.InjectEventSecurityException;
import androidx.test.espresso.PerformException;
import androidx.test.espresso.UiController;
import androidx.test.espresso.core.internal.deps.guava.base.Preconditions;

/* loaded from: classes.dex */
public final class MotionEvents {
    static final int MAX_CLICK_ATTEMPTS = 3;
    private static final String TAG = "MotionEvents";

    private MotionEvents() {
    }

    public static DownResultHolder sendDown(UiController uiController, float[] coordinates, float[] precision) {
        return sendDown(uiController, coordinates, precision, 0, 1);
    }

    public static MotionEvent obtainDownEvent(float[] coordinates, float[] precision, int inputDevice, int buttonState) {
        Preconditions.checkNotNull(coordinates);
        Preconditions.checkNotNull(precision);
        long uptimeMillis = SystemClock.uptimeMillis();
        if (Build.VERSION.SDK_INT < 14) {
            return downPressGingerBread(uptimeMillis, coordinates, precision);
        }
        return downPressICS(uptimeMillis, coordinates, precision, inputDevice, buttonState);
    }

    public static MotionEvent obtainDownEvent(float[] coordinates, float[] precision) {
        return obtainDownEvent(coordinates, precision, 0, 1);
    }

    public static DownResultHolder sendDown(UiController uiController, float[] coordinates, float[] precision, int inputDevice, int buttonState) {
        Preconditions.checkNotNull(uiController);
        Preconditions.checkNotNull(coordinates);
        Preconditions.checkNotNull(precision);
        int i = 0;
        while (true) {
            boolean z = true;
            if (i < 3) {
                try {
                    MotionEvent obtainDownEvent = obtainDownEvent(coordinates, precision, inputDevice, buttonState);
                    long downTime = obtainDownEvent.getDownTime();
                    long tapTimeout = (ViewConfiguration.getTapTimeout() / 2) + downTime;
                    boolean injectMotionEvent = uiController.injectMotionEvent(obtainDownEvent);
                    while (true) {
                        long uptimeMillis = tapTimeout - SystemClock.uptimeMillis();
                        if (uptimeMillis <= 10) {
                            break;
                        }
                        uiController.loopMainThreadForAtLeast(uptimeMillis / 4);
                    }
                    if (SystemClock.uptimeMillis() > downTime + ViewConfiguration.getLongPressTimeout()) {
                        Log.e(TAG, "Overslept and turned a tap into a long press");
                    } else {
                        z = false;
                    }
                    if (!injectMotionEvent) {
                        obtainDownEvent.recycle();
                        i++;
                    } else {
                        return new DownResultHolder(obtainDownEvent, z);
                    }
                } catch (InjectEventSecurityException e) {
                    throw new PerformException.Builder().withActionDescription("Send down motion event").withViewDescription(EnvironmentCompat.MEDIA_UNKNOWN).withCause(e).build();
                }
            } else {
                throw new PerformException.Builder().withActionDescription(String.format("click (after %s attempts)", 3)).withViewDescription(EnvironmentCompat.MEDIA_UNKNOWN).build();
            }
        }
    }

    public static boolean sendUp(UiController uiController, MotionEvent downEvent) {
        return sendUp(uiController, downEvent, new float[]{downEvent.getX(), downEvent.getY()});
    }

    public static MotionEvent obtainUpEvent(MotionEvent downEvent, float[] coordinates) {
        Preconditions.checkNotNull(downEvent);
        Preconditions.checkNotNull(coordinates);
        if (Build.VERSION.SDK_INT < 14) {
            return upPressGingerBread(downEvent, coordinates);
        }
        return upPressICS(downEvent, coordinates);
    }

    public static boolean sendUp(UiController uiController, MotionEvent downEvent, float[] coordinates) {
        Preconditions.checkNotNull(uiController);
        Preconditions.checkNotNull(downEvent);
        Preconditions.checkNotNull(coordinates);
        MotionEvent motionEvent = null;
        try {
            try {
                motionEvent = obtainUpEvent(downEvent, coordinates);
                if (!uiController.injectMotionEvent(motionEvent)) {
                    Log.e(TAG, String.format("Injection of up event failed (corresponding down event: %s)", downEvent.toString()));
                    return false;
                }
                if (motionEvent != null) {
                    motionEvent.recycle();
                }
                return true;
            } catch (InjectEventSecurityException e) {
                throw new PerformException.Builder().withActionDescription(String.format("inject up event (corresponding down event: %s)", downEvent.toString())).withViewDescription(EnvironmentCompat.MEDIA_UNKNOWN).withCause(e).build();
            }
        } finally {
            if (motionEvent != null) {
                motionEvent.recycle();
            }
        }
    }

    public static void sendCancel(UiController uiController, MotionEvent downEvent) {
        Preconditions.checkNotNull(uiController);
        Preconditions.checkNotNull(downEvent);
        MotionEvent motionEvent = null;
        try {
            try {
                motionEvent = MotionEvent.obtain(downEvent.getDownTime(), SystemClock.uptimeMillis(), 3, downEvent.getX(), downEvent.getY(), 0);
                if (!uiController.injectMotionEvent(motionEvent)) {
                    Log.e(TAG, String.format("Injection of cancel event failed (corresponding down event: %s)", downEvent.toString()));
                } else if (motionEvent != null) {
                    motionEvent.recycle();
                }
            } catch (InjectEventSecurityException e) {
                throw new PerformException.Builder().withActionDescription(String.format("inject cancel event (corresponding down event: %s)", downEvent.toString())).withViewDescription(EnvironmentCompat.MEDIA_UNKNOWN).withCause(e).build();
            }
        } finally {
            if (motionEvent != null) {
                motionEvent.recycle();
            }
        }
    }

    public static MotionEvent obtainMovement(long downTime, float[] coordinates) {
        return MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), 2, coordinates[0], coordinates[1], 0);
    }

    public static MotionEvent obtainMovement(long downTime, long eventTime, float[] coordinates) {
        return MotionEvent.obtain(downTime, eventTime, 2, coordinates[0], coordinates[1], 0);
    }

    public static boolean sendMovement(UiController uiController, MotionEvent downEvent, float[] coordinates) {
        Preconditions.checkNotNull(uiController);
        Preconditions.checkNotNull(downEvent);
        Preconditions.checkNotNull(coordinates);
        MotionEvent motionEvent = null;
        try {
            try {
                motionEvent = obtainMovement(downEvent.getDownTime(), coordinates);
                if (!uiController.injectMotionEvent(motionEvent)) {
                    Log.e(TAG, String.format("Injection of motion event failed (corresponding down event: %s)", downEvent.toString()));
                    return false;
                }
                if (motionEvent != null) {
                    motionEvent.recycle();
                }
                return true;
            } catch (InjectEventSecurityException e) {
                throw new PerformException.Builder().withActionDescription(String.format("inject motion event (corresponding down event: %s)", downEvent.toString())).withViewDescription(EnvironmentCompat.MEDIA_UNKNOWN).withCause(e).build();
            }
        } finally {
            if (motionEvent != null) {
                motionEvent.recycle();
            }
        }
    }

    private static MotionEvent downPressGingerBread(long downTime, float[] coordinates, float[] precision) {
        return MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), 0, coordinates[0], coordinates[1], 0.0f, 1.0f, 0, precision[0], precision[1], 0, 0);
    }

    private static MotionEvent downPressICS(long downTime, float[] coordinates, float[] precision, int inputDevice, int buttonState) {
        MotionEvent.PointerCoords[] pointerCoordsArr = {new MotionEvent.PointerCoords()};
        MotionEvent.PointerProperties[] pointerProperties = getPointerProperties(inputDevice);
        pointerCoordsArr[0].clear();
        pointerCoordsArr[0].x = coordinates[0];
        pointerCoordsArr[0].y = coordinates[1];
        pointerCoordsArr[0].pressure = 0.0f;
        pointerCoordsArr[0].size = 1.0f;
        return MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), 0, 1, pointerProperties, pointerCoordsArr, 0, buttonState, precision[0], precision[1], 0, 0, inputDevice, 0);
    }

    private static MotionEvent upPressGingerBread(MotionEvent downEvent, float[] coordinates) {
        return MotionEvent.obtain(downEvent.getDownTime(), SystemClock.uptimeMillis(), 1, coordinates[0], coordinates[1], 0);
    }

    private static MotionEvent upPressICS(MotionEvent downEvent, float[] coordinates) {
        MotionEvent.PointerCoords[] pointerCoordsArr = {new MotionEvent.PointerCoords()};
        MotionEvent.PointerProperties[] pointerProperties = getPointerProperties(downEvent.getSource());
        pointerCoordsArr[0].clear();
        pointerCoordsArr[0].x = coordinates[0];
        pointerCoordsArr[0].y = coordinates[1];
        pointerCoordsArr[0].pressure = 0.0f;
        pointerCoordsArr[0].size = 1.0f;
        return MotionEvent.obtain(downEvent.getDownTime(), SystemClock.uptimeMillis(), 1, 1, pointerProperties, pointerCoordsArr, 0, downEvent.getButtonState(), downEvent.getXPrecision(), downEvent.getYPrecision(), 0, 0, downEvent.getSource(), 0);
    }

    private static MotionEvent.PointerProperties[] getPointerProperties(int inputDevice) {
        MotionEvent.PointerProperties[] pointerPropertiesArr = {new MotionEvent.PointerProperties()};
        pointerPropertiesArr[0].clear();
        pointerPropertiesArr[0].id = 0;
        if (inputDevice == 4098) {
            pointerPropertiesArr[0].toolType = 1;
        } else if (inputDevice == 8194) {
            pointerPropertiesArr[0].toolType = 3;
        } else if (inputDevice == 16386) {
            pointerPropertiesArr[0].toolType = 2;
        } else {
            pointerPropertiesArr[0].toolType = 0;
        }
        return pointerPropertiesArr;
    }

    /* loaded from: classes.dex */
    public static class DownResultHolder {
        public final MotionEvent down;
        public final boolean longPress;

        DownResultHolder(MotionEvent down, boolean longPress) {
            this.down = down;
            this.longPress = longPress;
        }
    }
}
